(function() {
    'use strict';
    /**
     * 初始化部门列表的下拉列表数据
     */
    buildSelect.departmentList(".department");
    var checkedObject = {};
    /**
     * 公司员工列表  初始化渲染设置
     * @type {Object}
     */
    var settings = {
        "sAjaxSource": window.globalHost + "/agent-sys/admin/user/list.do",
        "aoColumns": [{
            "mDataProp": function(obj) {
                var currentStatus=obj.status;
                var currentLoginName=obj.loginName;
                if(currentStatus==="AVAILABLE"){
                    if(!!currentLoginName){
                        return "<a class='loginName' href='javascript:;' data-userId="+obj.id+">" + currentLoginName + "</a>";
                    }else{
                        return "<a class='loginName' href='javascript:;' data-userId="+obj.id+">" + "noLoginName" + "</a>";
                    }
                }
                if(currentStatus==="FORBID"){
                    return currentLoginName;
                }
            },
            "sTitle": "登录账号",
            "sDefaultContent": ""
        },{
            "mDataProp": "jobNumber",
            "sTitle": "工号",
            "sDefaultContent": ""
        }, {
                "mDataProp": 'departName',
            "sTitle": "部门",
            "sDefaultContent": ""
        }, {
            "mDataProp": "roleName",
            "sTitle": "职务",
            "sDefaultContent": ""
        }, {
            "mDataProp":"userName",
            "sTitle": "姓名",
            "sDefaultContent": ""
        }, {
            "mDataProp": "mobile",
            "sTitle": "手机号码",
            "sDefaultContent": ""
        }, {
            "mDataProp": "createTime",
            "sTitle": "创建时间",
            "sDefaultContent": ""
        },{
            "mDataProp": function (obj) {
                if(obj.status==="AVAILABLE"){
                    return "正常";
                }
                if(obj.status==="FORBID"){
                    return "已停用";
                }
            },
            "sTitle": "账号状态",
            "sDefaultContent": ""
        }, {
            "mDataProp": function (obj) {
                var currentStatus=obj.status;
                var currentEmployeeId=obj.id;
                if(currentStatus==="AVAILABLE"){
                    return  "<div class='col-sm-12'>" +
                                "<a href='javascript:;' data-userId="+currentEmployeeId+" class='resetPwd btn btn-success col-sm-5'>重置密码</a>"+
                                "<a href='javascript:;' data-userId="+currentEmployeeId+" class='stopUser btn btn-danger col-sm-5 pull-right'>停用</a>"
                            +"</div>";
                }
                if(currentStatus==="FORBID"){
                    return "<div class='col-sm-12'><a href='javascript:;' data-userId="+currentEmployeeId+" class='recoverUser btn btn-primary col-sm-5'>恢复</a></div>";
                }
            },
            "sTitle": "操作",
            "sClass": "",
            "sDefaultContent": ""
        }],
        "fnServerParams": function(aoData) {
            return common.getSearchData("form.employeeSearch",aoData);
        }
    };
    // $("body").on("click",".allClick",function(ev){
    //     var list = $("body").find("input[type='checkbox']");
    //     list.each(function(i,I){
    //         $(I).trigger('click')
    //     })
    // });
    var options = $.extend(true, {}, _dataTableSettings, settings);
    /**
     * 实例化公司员工列表插件
     * @type {[type]}
     */
    var $currentDataTable=$(".dataTables-example");
    var oTable = $currentDataTable.dataTable(options);
    /**
     * 绑定数据列表中的对应操作事件
     */
    $currentDataTable.off("click").on("click", ".loginName,.resetPwd,.stopUser,.recoverUser", function(ev) {
        var sendData={};
        var $this=$(this);
        var selfUserId=$this.attr("data-userId");
        //如果点击的是员工登录账号，则弹出更新员工详情信息页面
        if($this.hasClass("loginName")){
            common.openInsertDialog({
                'url': './basicInfo/employeeInfo.html?userId='+selfUserId,
                'title': '更新员工信息',
                'sAjaxSource': window.globalHost + '/agent-sys/admin/user/modifyUserInfo.do',
                'area': ['1000px', '600px'],
                ok: function(record) {
                    //更新成功之后，给予弹窗提示并刷新列表页面
                    var result=common.transformData(record);
                    layer.alert(result.res.msg);
                    oTable.fnClearTable();
                },
                cancel: function() {}
            });
        }
        //如果点击的是重置员工密码，则弹框提示确认是否重置，如果重置成功，员工的默认登录密码是123456
        if($this.hasClass("resetPwd")){
            common.openTempDialog({
                title:"重置员工用户密码",
                content:"<h4 class='dialogPageCenter text-center warnDanger'>是否要重置员工用户密码？</h4><div class='text-center warnDanger'>初始密码为123456</div>",
                ajaxUrl:window.globalHost+'/agent-sys/admin/user/modifyPwd.do',
                sendData:sendData,
                beforeSend:function (layerDocument) {
                    //组装发送数据，重置员工密码需要向后台传递的参数字段，id:当前员工的id，loginPwd:默认密码为123456，reloginPwd:重复密码，默认为123456
                    sendData.id=selfUserId;
                    sendData.loginPwd="123456";
                    sendData.reloginPwd="123456";
                    return !!sendData.id && !!sendData.loginPwd && sendData.reloginPwd;
                },
                resOk:function () {
                    //更新成功则刷新员工列表
                    oTable.fnClearTable();
                }
            });
        }
        //如果点击的是停用员工，则员工的状态为 已停用
        if($this.hasClass("stopUser")){
            changeEmployeeStatus("停用员工","FORBID");
        }
        //如果点击的是恢复员工，则员工的状态为正常
        if($this.hasClass("recoverUser")){
            changeEmployeeStatus("恢复员工","AVAILABLE");
        }
        function changeEmployeeStatus(dialogTitle,statusString) {
            common.openTempDialog({
                title:dialogTitle,
                content:"<h4 class='dialogPageCenter text-center warnDanger'>是否要"+dialogTitle+"？</h4>",
                ajaxUrl:window.globalHost+'/agent-sys/admin/user/stopOrStart.do',
                sendData:sendData,
                beforeSend:function (layerDocument) {
                    //组装发送数据，更新员工状态需要向后台传递的参数字段，id:当前员工的id，status:需要更改的员工目标状态
                    sendData.id=selfUserId;
                    sendData.status=statusString;
                    //返回发送数据条件，当前员工id和员工的目标状态不能为空
                    return !!sendData.id && !!sendData.status;
                },
                resOk:function () {
                    //更新成功则刷新员工列表
                    oTable.fnClearTable();
                }
            });
        }
    });
    /**
     * 绑定搜素按钮的点击事件、触发搜索动作。
     * @param  {[type]} ) {                   oTable.fnClearTable();        return false;    } [description]
     * @return {[type]}   [description]
     */
    $(".searchButton").click(function() {
        oTable.fnClearTable();
        return false;
    });
    /**
     * 绑定重置表单按钮的点击事件、触发重置表单控件。
     */
    $(".resetForm").click(function () {
        $('.result-selected').removeClass('result-selected');
        $('.chosen-single').children('span').html('请选择...');
    });
    /**
     * [新增公司员工弹框，common.openInsertDialog 是一个公共的弹框。专门用户处理 "新增页面弹框"]
     */
    $("button.insertEmployeeButton").off('click').on('click', function(ev) {
        common.openInsertDialog({
            'url': './basicInfo/insertEmployee.html',
            'title': '添加员工',
            'sAjaxSource': window.globalHost + '/agent-sys/admin/user/save.do',
            'area': ['1000px', '600px'],
            ok: function() {
                // 新增信息成功之后，刷新列表页面
                oTable.fnClearTable();
            },
            cancel: function() {}
        });
        ev.stopPropagation();
    });
}).call(this);

